<template>

  <div>

    <div class="search">
      <el-input  class="search-input"  v-model="search.searchContent" placeholder="请输入宠物名字" suffix-icon="el-icon-search"></el-input>
      <el-button type="primary" class="search-button"  plain @click="loadData()">搜索</el-button>
      <el-button type="warning"  plain @click="reset()">重置</el-button>
    </div>
    <div class="add-button">
    </div>
    <el-table
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}"
        :data="tableData"
        style="width: 100%">
      <el-table-column prop="id" label="ID" width="50"></el-table-column>
      <el-table-column label="头像">
        <template v-slot="scope">
          <el-image :src="scope.row.pic" :preview-src-list="[scope.row.pic]" class="table-img"></el-image>
        </template>
      </el-table-column>

      <el-table-column prop="name" label="名字" > </el-table-column>
      <el-table-column prop="type" label="种类"> </el-table-column>
      <el-table-column prop="sex" label="性别" ></el-table-column>
      <el-table-column prop="age" label="年龄" ></el-table-column>
      <el-table-column prop="state" label="状态"> </el-table-column>
      <el-table-column prop="owner" label="主人"> </el-table-column>
      <el-table-column prop="" label="操作" width="160">
        <template slot-scope="scope">
          <el-button type="primary"  icon="" @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger"  @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div  style="margin-top: 20px">
      <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="请填写信息" :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="form" label-position="right" label-width="100px" style="padding-right: 40px">
        <el-form-item label="名字">
          <el-input  v-model="form.name" placeholder="请输入名字"></el-input>
        </el-form-item>
        <el-form-item label="种类">
          <el-select v-model="form.type" placeholder="请选择种类">
            <el-option label="犬类" value="犬类"></el-option>
            <el-option label="猫类" value="猫类"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group  v-model="form.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input  v-model="form.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-input  v-model="form.state" placeholder="请输入状态" disabled></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input  v-model="form.remark" placeholder="请输入备注"></el-input>
        </el-form-item>
        <el-form-item label="图片">

          <el-upload
              class="avatar-uploader"
              action="http://localhost:9090/files/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess">
            <img v-if="form.pic" :src="form.pic" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>

        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  @click="dialogFormVisible = false">取 消</el-button>
        <el-button  type="primary" @click="save">保 存</el-button>
      </div>
    </el-dialog>

  </div>


</template>

<script>

import request from "@/utils/request";
export default {
  name: 'User',

  data() {
    return {
      user: JSON.parse(localStorage.getItem('userInfo') || '{}'),
      form:{},
      search:{},
      dialogFormVisible:false,
      dialogVisible:false,
      input:'',
      pageNum:1,
      pageSize:6,
      total:0,
      searchContent:'',
      tableData: []
    }
  },
  created() {
    this.loadData()
  },
  methods:{
    //  当点击页码时，当前页码数赋值给data变量pageNum
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.loadData()
    },
    // 分页加载表格数据
    loadData() {
      request.get("/pet/selectPage",{
        params:{
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          name:this.search.searchContent
        }
      }).then(res=>{
        if (res.code=='0'){
          this.tableData = res.data.list
          this.total=res.data.total
        }
        else {
          this.$message.error(res.msg)
        }
      })
    },
    reset(){
      this.search.username = null
      this.loadData()
    },
    add(){
      this.form = {}
      this.dialogFormVisible=true
    },

    save() {
      if (!this.form.id) {
        //新增 为空
        request.post("pet/save", this.form).then(res => {
          if (res.code === '0') {
            this.$message.success('保存成功');
            this.loadData()
            this.dialogFormVisible = false
          }
        })
      } else {
        request.post("pet/update", this.form).then(res => {
          if (res.code === '0') {
            this.$message.success('修改成功');
            this.loadData()
            this.dialogFormVisible = false
          }

        })
      }
    },

    del(id){
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        request.delete('/pet/delete/' + id).then(res => {
          if (res.code === '0') {   // 表示操作成功
            this.$message.success('操作成功')
            this.loadData()
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      })
          .catch(()=>{})
    },
    edit(row){
      this.form={}
      this.form=row
      this.dialogFormVisible=true
    },

    handleAvatarSuccess(response) {
      // 获取后台传来的图片路径
      this.form.pic = response.data
      this.$forceUpdate()
    },
  }
}
</script>

<style scoped>

</style>